package com.application.lucky.manager;

import android.text.TextUtils;

import com.application.lucky.Lucky;
import com.application.lucky.bean.eventbean.FriendListEvent;
import com.application.lucky.bean.jsonBean.FriendBean;
import com.application.lucky.bean.jsonBean.FriendListbean;
import com.application.lucky.gen.FriendBeanDao;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.FriendRequest;
import com.application.lucky.utils.IMCache;
import com.application.lucky.utils.LocalGroupSearch;
import com.application.mylibrary.utils.EventBusUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

import java.util.ArrayList;
import java.util.List;

public class ContactsManager {

    private final IMCache<String, FriendBean> users;
    private final FriendBeanDao dao;
//    private final List<FriendBean> friendBeans;

    private ContactsManager() {
        dao = Lucky.getApplication().getDaoSession().getFriendBeanDao();

        friends = new ArrayList<>();
        users = new IMCache<>(255);
    }

    private static class Inner {
        public static ContactsManager manager = new ContactsManager();
    }

    public static ContactsManager getInstance() {
        return Inner.manager;
    }

    private List<FriendListbean.FriendListBean> friends;

    public void addData(List<FriendListbean.FriendListBean> datas) {
        if (datas != null) {
            for (FriendListbean.FriendListBean data : datas) {
                if (data.getUser() != null) {
                    users.put(data.getUserId() + "", data.getUser());
                    dao.insertOrReplace(data.getUser());
                }
            }
            friends = datas;
        }

    }

    public void addData(FriendListbean.FriendListBean data) {
        users.put(data.getUserId() + "", data.getUser());
    }


    public FriendBean searchFriendsByid(String name) {
        FriendBean bean = users.get(name);
        if (bean == null) {
            bean = dao.load(Long.valueOf(name));
        }
        return bean;

    }

    public List<FriendListbean.FriendListBean> search(String s) {
        return LocalGroupSearch.searchGroup(s, (ArrayList<FriendListbean.FriendListBean>) friends);
    }

    public boolean isFriend(String id) {
        if (TextUtils.isEmpty(id)) {
            return false;
        }
        FriendBean bean = users.get(id);
        return bean != null;
    }

    public void loadFriends() {
        new FriendRequest().friend_list("0", new CallBackEx<BaseResponse<FriendListbean>>() {
            @Override
            public void onSuccess(BaseResponse<FriendListbean> friendListbeanBaseResponse) {
//                refres/hView.setFinish();
                FriendListbean dataInfo = friendListbeanBaseResponse.dataInfo;

                if (dataInfo != null) {
                    EventBusUtils.post(new FriendListEvent());
                    List<FriendListbean.FriendListBean> friendList = dataInfo.getFriendList();
//                    List<FriendBean> beans = new ArrayList<>();

                    addData(friendList);

                }

            }

            @Override
            public void onFailed(int code) {

            }


        });
    }
}
